﻿using Assets.Scripts.DL.Consts;
using Assets.Scripts.DL.Enemy.CommonEnemy;
using Assets.Scripts.DL.Mgrs;
using UnityEngine;

namespace Assets.Scripts.DL.Enemy.SpriderBoss {
	public class SpiderBossCommonAtk : CommonState<SpiderBossController> {
		readonly Timer _timer;
		bool _sendDamage;
		//bool _animStart;

		Vector2 _atkOffset;
		readonly float _range;
		readonly int _damage;
		float _DmgSendTime;
		public SpiderBossCommonAtk(SpiderBossController controller, StateEnum stateName) : base(controller, stateName) {
			_timer = _controller.StateData.AtkCdRecoder[StateEnum.Atk1];//同一个计时器

			if (StateName == StateEnum.Atk1) {
				_atkOffset = _controller.Atk1Off;
				_range = _controller.Atk1Range;
				_damage = _controller.Atk1Dmg;
				_DmgSendTime = _controller.Atk1NormalTime;
			}
			else {
				_atkOffset = _controller.Atk2Off;
				_range = _controller.Atk2Range;
				_damage = _controller.Atk2Dmg;
				_DmgSendTime = _controller.Atk2NormalTime;
			}

			AddConditionToSelf(() => _timer.Elapsed > _controller.Config.CommonAtkGap);
		}

		public override void Enter() {
			_controller.Player.GetComponent<PlayerController>().ShowDangerIcon();
			_controller.PlayAnim(StateName);
			_controller.Velocity = Vector2.zero;
			_controller.StateData.AtkOver = false;
			_sendDamage = false;
		}

		public override void Update() {
			if (_controller.IsAnimPlayAt(_DmgSendTime) && !_sendDamage) {
				CheckDamage(ref _sendDamage);
				AudioManager.Play(_controller.StateAudio, AudioStr.Poison);
			}
			if (_controller.IsAnimPlayOver()) {
				_controller.StateData.AtkOver = true;
			}
		}

		public override void Exit() {
			_timer.Reset();
			//_controller.StateData.LastAtkTime.Reset();
		}

		void CheckDamage(ref bool dmgSend) {
			//if (_controller.IsAnimPlayAt(0.65f)) {
			_controller.CheckAtk(_atkOffset, _range, _damage);
			dmgSend = true;
		}
	}
}

